package com.sec.etech.common.service;

import com.sec.etech.common.dao.SerialCode;
import com.sec.etech.common.dao.SubSerialCode;
import org.openbpm.base.core.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDate;


/**
 * @version : 1.0
 * @auther : 老邓
 * @date : 2020/10/23 10:19
 * @description:
 */
@Service
public class SerialCodeService {

    @Autowired
    JdbcTemplate jdbcTemplate;

    //获取主流水编号
    public String getSerialCode(SerialCode serialCode) {
        String lastCode;
        Integer num = serialCode.getScope().length();
        String year = String.valueOf(LocalDate.now().getYear());
        String marking = StringUtil.isEmpty(serialCode.getMarking()) ? "" : serialCode.getMarking();
        String sql = "SELECT " + serialCode.getCodeKey() +
                " FROM " + serialCode.getDtName() +
                " WHERE " + serialCode.getCodeKey() + " LIKE '" + serialCode.getScope() + year + marking + "%'" +
                " AND CHAR_LENGTH(" + serialCode.getCodeKey() + ")<=" + (num + 4 + marking.length() + serialCode.getCodeNum()) +
                " ORDER BY " + serialCode.getCodeKey() + " DESC LIMIT 1";
        try {
            lastCode = jdbcTemplate.queryForObject(sql, String.class);
        } catch (DataAccessException e) {
            lastCode = null;
        }
        if (lastCode == null || lastCode.length() <= (num + 3) || (lastCode.length() > (num + 4) && !lastCode.substring(num, (num + 4)).equals(year))) {
            return serialCode.getScope() + year + marking +
                    String.format("%" + serialCode.getCodeNum() + "d", 1).replace(" ", "0");
        } else {
            return serialCode.getScope() + year + marking +
                    String.format("%" + serialCode.getCodeNum() + "d", Integer.valueOf(lastCode.substring(lastCode.length() - serialCode.getCodeNum())) + 1).replace(" ", "0");
        }
    }

    //获取子流水编号
    public String getSubSerialCode(SubSerialCode subSerialCode) {
        String sql = "SELECT " + subSerialCode.getCodeKey() + " FROM " + subSerialCode.getDtName() +
                " WHERE " + subSerialCode.getIdKey() + "=" + subSerialCode.getMainid();
        String mainnum = jdbcTemplate.queryForObject(sql, String.class);
        String lastCode;
        sql = "SELECT " + subSerialCode.getCodeKey() + " FROM " + subSerialCode.getDtName() +
                " WHERE " + subSerialCode.getCodeKey() + " LIKE '" + mainnum + subSerialCode.getLinkCode() + "%' ORDER BY " +
                subSerialCode.getCodeKey() + " DESC LIMIT 1";
        try {
            lastCode = jdbcTemplate.queryForObject(sql, String.class);
        } catch (DataAccessException e) {
            lastCode = null;
        }
        if (lastCode == null || lastCode.length() != (mainnum.length() + subSerialCode.getLinkCode().length() + subSerialCode.getSubCodeNum())) {
            return mainnum + subSerialCode.getLinkCode() +
                    String.format("%" + subSerialCode.getSubCodeNum() + "d", 1).replace(" ", "0");
        } else {
            return mainnum + subSerialCode.getLinkCode() +
                    String.format("%" + subSerialCode.getSubCodeNum() + "d", Integer.valueOf(lastCode.substring(lastCode.length() - subSerialCode.getSubCodeNum())) + 1).replace(" ", "0");
        }
    }
}
